package com.steak.concurrent.Future.futuretask;

import com.steak.concurrent.common.OrderInfo;
import com.steak.concurrent.common.R;

import java.util.concurrent.*;

/**
 * @author 刘牌
 * @date 2022/3/2617:34
 */
public class PlaceOrderFutureTaskTest {
    final static ExecutorService threadPool = Executors.newCachedThreadPool();

    //保存订单任务
    public static FutureTask<R> saveOrderTask(OrderInfo orderInfo){
        return new FutureTask<>(new Callable<R>() {
            @Override
            public R call() throws Exception {
                return saveOrder(orderInfo);
            }
        });
    }

    //扣减库存任务
    public static FutureTask<R> decreaseStockTask(OrderInfo orderInfo){
        return new FutureTask<>(new Callable<R>() {
            @Override
            public R call() throws Exception {
                return decreaseStockByCommodityId(orderInfo);
            }
        });
    }

    //增加积分任务
    public static FutureTask<R> increaseIntegralTask(OrderInfo orderInfo){
        return new FutureTask<>(new Callable<R>() {
            @Override
            public R call() throws Exception {
                return increaseIntegralByUserId(orderInfo);
            }
        });
    }

    public static void sendMsgToPhone(OrderInfo orderInfo){
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("用户【"+orderInfo.getUserId()+"】，你已下单成功~~~~~~~~");
            }
        });
    }

    //增加积分rpc接口
    public static R increaseIntegralByUserId(OrderInfo orderInfo){
        System.out.println("增加积分~~~~~~~~");
        //integralService.increaseIntegralByUserId(orderInfo.getUserId(),20);
        return new R(200,"增加积分成功",null);
    }

    //扣减库存rpc接口
    public static R decreaseStockByCommodityId(OrderInfo orderInfo){
        System.out.println("扣减库存~~~~~~~~");
        //stockService.decreaseStockByCommodityId(orderInfo.getCommodityId());
        return new R(200,"扣减库存成功",null);
    }

    //保存订单rpc接口
    public static R saveOrder(OrderInfo orderInfo) throws InterruptedException {
        System.out.println("保存订单~~~~~~~~");
        Thread.sleep(2000);
        //orderService.insert(orderInfo);
        return new R(200,"保存订单成功",null);
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException, TimeoutException {
        OrderInfo orderInfo = new OrderInfo().setId("123455").setUserId("111111").setCommodityId("123321");
        FutureTask<R> orderTask = saveOrderTask(orderInfo);
        FutureTask<R> stockTask = decreaseStockTask(orderInfo);
        FutureTask<R> integralTask = increaseIntegralTask(orderInfo);
        threadPool.submit(orderTask);
        threadPool.submit(stockTask);
        threadPool.submit(integralTask);
        sendMsgToPhone(orderInfo);
        if (orderTask.get().getCode() == 200 && orderTask.isDone()) System.out.println(orderTask.get().getMsg());
        if (stockTask.get().getCode() == 200 && stockTask.isDone()) System.out.println(stockTask.get().getMsg());
        if (integralTask.get().getCode() == 200 && integralTask.isDone()) System.out.println(integralTask.get().getMsg());
        threadPool.shutdownNow();
    }
}
